<template>
  <div class="loginContainer">
    <el-card header="登录" class="login">
      <el-form
        :model="formData"
        status-icon
        :rules="rules"
        ref="formData"
        label-width="100px"
        label-position="left"
      >
        <el-form-item class="loginlabel" label="用户名" prop="username">
          <el-input
            class="logininput"
            type="text"
            v-model="formData.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item class="loginlabel" label="密码" prop="password">
          <el-input
            type="password"
            v-model="formData.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="submitForm('formData')"
            >登录</el-button
          >
          <el-button type="success" @click="resetForm('formData')"
            >清空</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>


<script>
import { login } from "../api/users";
import {setToken} from "../Utils/auth"
export default {
  data() {
    return {
      formData: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 获取后端用户名密码传入前端
          login(this.formData)
            .then(({ data }) => {
              if (data.code === 1000) {
                //传入token和refreshToken
                setToken(data.data.token,data.data.refreshToken);
                // localStorage.setItem("token", data.data.token),
                // localStorage.setItem("refreshToken", data.data.refreshToken);
                this.$router.push("/");
              } else {
                this.$message({ type: "warning", message: data.msg });
                this.$notify.success({
                  title: "Info",
                  message: "用户名或密码错误啊啊啊",
                });
                this.$notify({
                  type: "success",
                  title: "错误",
                  message: "用户名或密码错误啊啊啊",
                  position: "bottom-right",
                });
                this.$notify({
                  type: "warning",
                  title: "错误",
                  message: "用户名或密码错误啊啊啊",
                  position: "bottom-left",
                });
                this.$notify({
                  type: "Info",
                  title: "错误",
                  message: "用户名或密码错误啊啊啊",
                  position: "top-left",
                });
                this.$notify({
                  type: "error",
                  title: "错误",
                  message: "用户名或密码错误啊啊啊",
                  position: "bottom-right",
                });
                // alert("用户名或密码不正确")
              }
              console.log(data);
            })
            .catch((err) => {
              this.$message({ type: "success", message: err });
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },

};
</script>

<style scoped>
.loginlabel {
  font-weight: 900;
}
.login {
  /* background-image: url(../img/6.jpg); */
  background-size: 100% 100%;
  background-color: rgba(201, 185, 185, 0.5);
  /* Background:rgba(255,2,2,.3) */
}
.loginContainer {
  background-image: url(../img/7.jpg);
  background-size: 50% 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 80vh;
}
.el-card {
  width: 500px;
}
.el-card__header {
  font-size: 30px;
}
</style>

